<template>
  <el-dialog
    width="40%"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="100px">
      <el-form-item label="审核结果：">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核意见：">
        <el-input type="textarea" :rows="4" :autosize="{ minRows: 6, maxRows: 8}" placeholder="系统默认信息" v-model="formData.opinion" ></el-input>
      </el-form-item>
    </el-form>
    <el-row style="margin-top:17px; ">
      <el-button style="float:right;margin-left:6px;" size="small" type="danger" @click="handleClose">取 消</el-button>
      <el-button style="float:right" size="small" type="primary" @click="submitForm('formData')">确 定</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
  import { updateAuditById } from '@/api/audit'
  export default {
    props: {
      formData: {
        type: Object,
        default: () => {}
      },
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    methods: {
      handleClose(done) {
        this.$refs.formData.resetFields();
        this.$emit('close-callback')
      },
      submitForm(formData) {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            if (this.formData.opinion === null) {
              this.formData.opinion = "系统默认信息"
            }
            if (this.formData.id === undefined) {
              this.$message({
                type: 'error',
                message: "审核失败！"
              });
              this.handleClose()
            }
            updateAuditById(this.formData.id, this.$qs.stringify(this.formData))
              .then(res => {
              if (res.code === 20000) {
                // 提交成功, 关闭窗口, 刷新列表
                this.$message({
                  type: 'success',
                  message: "审核成功！"
                });
                this.handleClose()
              }
            }).catch(() => {
            })
          } else {
            this.$message({
              type: 'error',
              message: "审核失败！"
            });
            this.handleClose()
          }
        })
      }
    }
  }
</script>
